var parse = require('url').parse;
/**
 * [route description]
 * @param  {[type]} object [{'GET':{},'POST':{},'PUT':{},'DELETE':{}}路由对象]
 * @return {[type]}        [description]
 */
function route(object) {
    return function(req, res, next) {
        if (!object[req.method]) {
            return next();
        } else {
            var routes = object[req.method];
            var url = parse(req.url);
            var paths = Object.keys(routes);
            for (var i = paths.length - 1; i >= 0; i--) {
                var path = paths[i]; //key
                var fn = routes[path]; //callback
                path = path.replace(/\//g, '\\/').replace(/\:(\w+)/g, '([^\\/]+)');
                var regbx = new RegExp('^' + path + '$');
                var captures = url.pathname.match(regbx);            
                if (captures) {
                    var args = [req, res];
                    fn.apply(null, args);
                    return;
                }
            }
            next();
        }
    }
}
module.exports = route;